package com.example;

import com.alibaba.cloud.commons.lang.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.web.client.RestTemplate;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class AuthorizationServerApplication {

//    @Bean
//    InitializingBean saveData(LocationRepository repo) {
//        return () -> {
//            repo.save(new Location((long) 1, "1", 38.998064, 117.317267));
//            repo.save(new Location((long) 2, "2", 38.997793, 117.317069));
//            repo.save(new Location((long) 3, "3", 38.998006, 117.317101));
//            repo.save(new Location((long) 4, "4", 38.997814, 117.317332));
//        };
//    }

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }


    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(AuthorizationServerApplication.class, args);
        Environment env = application.getEnvironment();
        String contextPath = env.getProperty("server.servlet.context-path");
        if(StringUtils.isBlank(contextPath)){
            contextPath = "";
        }
        log.info("\n----------------------------------------------------------\n\t" +
                        "Application '{}' is running! Access URLs:\n\t" +
                        "Local: \t\thttp://localhost:{}\n\t" +
                        "External: \thttp://{}:{}\n\t"+
                        "Doc: \thttp://{}:{}/doc.html\n"+
                        "----------------------------------------------------------",
                env.getProperty("spring.application.name"),
                env.getProperty("server.port"),
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port") + contextPath,
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port") + contextPath);
    }

}
